package cn.yangliu.springbatch.config;

import cn.yangliu.springbatch.entity.BatchJobExecutionSeq;
import cn.yangliu.springbatch.entity.BatchJobSeq;
import cn.yangliu.springbatch.entity.BatchStepExecutionSeq;
import cn.yangliu.springbatch.entity.Role;
import cn.yangliu.springbatch.entity.User;
import cn.yangliu.springbatch.repository.BatchJobExecutionSeqRepository;
import cn.yangliu.springbatch.repository.BatchJobSeqRepository;
import cn.yangliu.springbatch.repository.BatchStepExecutionSeqRepository;
import cn.yangliu.springbatch.repository.RoleRepository;
import cn.yangliu.springbatch.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * the type of springbatch
 *
 * <p>
 * .
 *
 * @author 杨柳
 * @date 2021-07-13
 */
@Component
public class InitHandler {

    @Autowired
    private BatchJobExecutionSeqRepository batchJobExecutionSeqRepository;

    @Autowired
    private BatchJobSeqRepository batchJobSeqRepository;

    @Autowired
    private BatchStepExecutionSeqRepository batchStepExecutionSeqRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RoleRepository roleRepository;

    @PostConstruct
    public void init(){
        if (batchJobExecutionSeqRepository.count() == 0) {
            batchJobExecutionSeqRepository.save(new BatchJobExecutionSeq(0L, "0"));
        }
        if (batchJobSeqRepository.count() == 0) {
            batchJobSeqRepository.save(new BatchJobSeq(0L, "0"));
        }
        if (batchStepExecutionSeqRepository.count() == 0) {
            batchStepExecutionSeqRepository.save(new BatchStepExecutionSeq(0L, "0"));
        }
        if (userRepository.count() == 0) {
            userRepository.save(new User("杨柳", 30, "双流", "java开发"));
            userRepository.save(new User("夏云红", 24, "双流", "java开发"));
            userRepository.save(new User("李甜甜", 24, "高新", "前端开发"));
        }

        if (roleRepository.count() == 0) {
            roleRepository.save(new Role( "java开发"));
            roleRepository.save(new Role("前端开发"));
        }
    }
}
